package servlet;

import Utils.JDBCUtil;
import dao.OrderDao;
import dao.ShopDao;
import pojo.Course;
import pojo.Order;
import pojo.Shop;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/list", "/delete", "/checkoutpage", "/order", "/updateStatus"})
public class ShopServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String servletPath = request.getServletPath();
        if ("/list".equals(servletPath)) {
            doList(request, response);
        } else if ("/delete".equals(servletPath)) {
            doDel(request, response);
        } else if ("/checkoutpage".equals(servletPath)) {
            doCheck(request, response);
        } else if ("/order".equals(servletPath)) {
            doAddOrder(request, response);
        } else if ("/updateStatus".equals(servletPath)) {
            doUpdate(request, response);
        }
    }

    private void doAddOrder(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        ShopDao shopDao = new ShopDao();
        OrderDao orderDao = new OrderDao();
        List<String> orders = new ArrayList<>();

        List<Course> goodsList = shopDao.findGoods(user.getAccount());

        for (Course good : goodsList) {
            String id = user.getAccount() + System.currentTimeMillis() + new String(good.getId());
            orderDao.insertOne(new Order(id, good.getId(), user.getAccount()));
            orders.add(id);
        }
        request.getSession().setAttribute("orders", orders);
    }


    private void doDel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取课程编号
        String shopid = request.getParameter("shopid");
        //连接数据库,删除课程
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "delete from shop where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, shopid);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(null, ps, conn);
        }
        if (count == 1) {
            //更新成功
            //重定向到列表页面
            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath + "/shop/list");
        }
    }


    private void doCheck(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        ShopDao shopDao = new ShopDao();
        List<Course> shopList = shopDao.findGoods(user.getAccount());
        request.setAttribute("goodsList", shopList);
        request.getRequestDispatcher("/checkoutpage.jsp").forward(request, response);
    }


    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else {
            ShopDao shopDao = new ShopDao();
            List<Shop> shopList = shopDao.findShop(user.getAccount());
            request.setAttribute("shopList", shopList);
            request.getRequestDispatcher("shopcar.jsp").forward(request, response);
        }
    }

    private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        List<String> orders = (List<String>) request.getSession().getAttribute("orders");
        OrderDao orderDao = new OrderDao();
        for (String id : orders) {
            orderDao.changeOne(1, id);
        }
    }
}